home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / TinyGL / ami / content / ad709 / tinygl / src / zgl.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-15  |  8.3 KB  |  358 lines

  1. #ifndef _tgl_zgl_h_
  2. #define _tgl_zgl_h_
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <math.h>
  7. #include <assert.h>
  8. #include <ad709/tinygl/gl.h>
  9. #include "zbuffer.h"
  10. #include "zmath.h"
  11. #include "zfeatures.h"
  12.  
  13.  
  14.  
  15. #define DEBUG
  16. /* #define NDEBUG */
  17.  
  18. enum {
  19.     
  20. #define ADD_OP(a,b,c) OP_ ## a , 
  21. #include "opinfo.h"
  22.     
  23. };
  24.  
  25.  
  26. /* initially # of allocated GLVertexes (will grow when necessary) */
  27. #define POLYGON_MAX_VERTEX 16
  28.  
  29. /* Max # of specular light pow buffers */
  30. #define MAX_SPECULAR_BUFFERS 8
  31. /* # of entries in specular buffer */
  32. #define SPECULAR_BUFFER_SIZE 1024
  33. /* specular buffer granularity */
  34. #define SPECULAR_BUFFER_RESOLUTION 1024
  35.  
  36.  
  37. #define MAX_MODELVIEW_STACK_DEPTH  32
  38. #define MAX_PROJECTION_STACK_DEPTH 8
  39. #define MAX_TEXTURE_STACK_DEPTH      8
  40. #define MAX_NAME_STACK_DEPTH          64
  41. #define MAX_TEXTURE_LEVELS           11
  42. #define MAX_LIGHTS                      16
  43.  
  44. #define VERTEX_HASH_SIZE 1031
  45.  
  46. #define MAX_DISPLAY_LISTS 1024
  47. #define OP_BUFFER_MAX_SIZE 512
  48.  
  49. #define TGL_OFFSET_FILL     0x1
  50. #define TGL_OFFSET_LINE     0x2
  51. #define TGL_OFFSET_POINT    0x4
  52.  
  53. typedef struct GLSpecBuf {
  54.     int shininess_i;
  55.     int last_used;
  56.     float buf[SPECULAR_BUFFER_SIZE+1];
  57.     struct GLSpecBuf *next;
  58. } GLSpecBuf;
  59.  
  60. typedef struct GLLight {
  61.     V4 ambient;
  62.     V4 diffuse;
  63.     V4 specular;
  64.     V4 position;    
  65.     V3 spot_direction;
  66.     float spot_exponent;
  67.     float spot_cutoff;
  68.     float attenuation[3];
  69.     /* precomputed values */
  70.     float cos_spot_cutoff;
  71.     V3 norm_spot_direction;
  72.     V3 norm_position;
  73.     /* we use a linked list to know which are the enabled lights */
  74.     int enabled;
  75.     struct GLLight *next,*prev;
  76. } GLLight;
  77.  
  78. typedef struct GLMaterial {
  79.     V4 emission;
  80.     V4 ambient;
  81.     V4 diffuse;
  82.     V4 specular;
  83.     float shininess;
  84.     
  85.     /* computed values */
  86.     int shininess_i;
  87.     int do_specular;    
  88. } GLMaterial;
  89.  
  90.  
  91. typedef struct GLViewport {
  92.     int xmin,ymin,xsize,ysize;
  93.     V3 scale;
  94.     V3 trans;
  95.     int updated;
  96. } GLViewport;
  97.  
  98. typedef union {
  99.     int op;
  100.     float f;
  101.     int i;
  102.     unsigned int ui;
  103.     void *p;
  104. } GLParam;
  105.  
  106. typedef struct GLParamBuffer {
  107.     GLParam ops[OP_BUFFER_MAX_SIZE];
  108.     struct GLParamBuffer *next;
  109. } GLParamBuffer;
  110.  
  111. typedef struct GLList {
  112.     GLParamBuffer *first_op_buffer;
  113.     /* TODO: extensions for an hash table or a better allocating scheme */
  114. } GLList;
  115.  
  116. typedef struct GLVertex {
  117.     int edge_flag;
  118.     V3 normal;
  119.     V4 coord;
  120.     V4 tex_coord;
  121.     V4 color;
  122.     
  123.     /* computed values */
  124.     V4 ec;                     /* eye coordinates */
  125.     V4 pc;                     /* coordinates in the normalized volume */
  126.     int clip_code;          /* clip code */
  127.     ZBufferPoint zp;         /* integer coordinates for the rasterization */
  128. } GLVertex;
  129.  
  130. typedef struct GLImage {
  131.     void *pixmap;
  132.     int xsize,ysize;
  133. } GLImage;
  134.  
  135. /* textures */
  136.  
  137. #define TEXTURE_HASH_TABLE_SIZE 256
  138.  
  139. typedef struct GLTexture {
  140.     GLImage images[MAX_TEXTURE_LEVELS];
  141.     int handle;
  142.     struct GLTexture *next,*prev;
  143. } GLTexture;
  144.  
  145.  
  146. /* shared state */
  147.  
  148. typedef struct GLSharedState {
  149.     GLList **lists;
  150.     GLTexture **texture_hash_table;
  151. } GLSharedState;
  152.  
  153. struct GLContext;
  154.  
  155. typedef void (*gl_draw_triangle_func)(struct GLContext *c,
  156.                                       GLVertex *p0,GLVertex *p1,GLVertex *p2);
  157.  
  158. /* display context */
  159. typedef struct GLContext {
  160.     /* Z buffer */
  161.     ZBuffer *zb;
  162.     
  163.     /* lights */
  164.     GLLight lights[MAX_LIGHTS];
  165.     GLLight *first_light;
  166.     V4 ambient_light_model;
  167.     int local_light_model;
  168.     int lighting_enabled;
  169.     int light_model_two_side;
  170.     
  171.     /* materials */
  172.     GLMaterial materials[2];
  173.     int color_material_enabled;
  174.     int current_color_material_mode;
  175.     int current_color_material_type;
  176.     
  177.     /* textures */
  178.     GLTexture *current_texture;
  179.     int texture_2d_enabled;
  180.     
  181.     /* shared state */
  182.     GLSharedState shared_state;
  183.     
  184.     /* current list */
  185.     GLParamBuffer *current_op_buffer;
  186.     int current_op_buffer_index;
  187.     int exec_flag,compile_flag,print_flag;
  188.     
  189.     /* matrix */
  190.     
  191.     int matrix_mode;
  192.     M4 *matrix_stack[3];
  193.     M4 *matrix_stack_ptr[3];
  194.     int matrix_stack_depth_max[3];
  195.     
  196.     M4 matrix_model_view_inv;
  197.     M4 matrix_model_projection;
  198.     int matrix_model_projection_updated;
  199.     int matrix_model_projection_no_w_transform; 
  200.     int apply_texture_matrix;
  201.     
  202.     /* viewport */
  203.     GLViewport viewport;
  204.     
  205.     /* current state */
  206.     int polygon_mode_back;
  207.     int polygon_mode_front;
  208.     
  209.     int current_front_face;
  210.     int current_shade_model;
  211.     int current_cull_face;
  212.     int cull_face_enabled;
  213.     int normalize_enabled;
  214.     int blending_enabled;
  215.     gl_draw_triangle_func draw_triangle_front, draw_triangle_back;
  216.     
  217.     /* selection */
  218.     int render_mode;
  219.     unsigned int *select_buffer;
  220.     int select_size;
  221.     unsigned int *select_ptr,*select_hit;
  222.     int select_overflow;
  223.     int select_hits;
  224.     
  225.     /* names */
  226.     unsigned int name_stack[MAX_NAME_STACK_DEPTH];
  227.     int name_stack_size;
  228.     
  229.     /* clear */
  230.     float clear_depth;
  231.     V4 clear_color;
  232.     
  233.     /* current vertex state */
  234.     V4 current_color;
  235.     unsigned int longcurrent_color[3]; /* precomputed integer color */
  236.     V4 current_normal;
  237.     V4 current_tex_coord;
  238.     int current_edge_flag;
  239.     
  240.     /* glBegin / glEnd */
  241.     int in_begin;
  242.     int begin_type;
  243.     int vertex_n,vertex_cnt;
  244.     int vertex_max;
  245.     GLVertex *vertex;
  246.     
  247.     /* opengl 1.1 arrays  */
  248.     float *vertex_array;
  249.     int vertex_array_size;
  250.     int vertex_array_stride;
  251.     float *normal_array;
  252.     int normal_array_stride;
  253.     float *color_array;
  254.     int color_array_size;
  255.     int color_array_stride;
  256.     float *texcoord_array;
  257.     int texcoord_array_size;
  258.     int texcoord_array_stride;
  259.     int client_states;
  260.     
  261.     /* opengl 1.1 polygon offset */
  262.     float offset_factor;
  263.     float offset_units;
  264.     int offset_states;
  265.     
  266.     /* specular buffer. could probably be shared between contexts, 
  267.     but that wouldn't be 100% thread safe */
  268.     GLSpecBuf *specbuf_first;
  269.     int specbuf_used_counter;
  270.     int specbuf_num_buffers;
  271.     
  272.     /* opaque structure for user's use */
  273.     void *opaque;
  274.     /* resize viewport function */
  275.     int (*gl_resize_viewport)(struct GLContext *c,int *xsize,int *ysize);
  276.     
  277.     /* depth test */
  278.     int depth_test;
  279. } GLContext;
  280.  
  281. extern GLContext *gl_ctx;
  282.  
  283. void glDebug(int mode);
  284. void glInit(void *zbuffer);
  285. void glClose(void);
  286.  
  287.  
  288. void gl_add_op(GLParam *p);
  289.  
  290. /* clip.c */
  291. void gl_transform_to_viewport(GLContext *c,GLVertex *v);
  292. void gl_draw_triangle(GLContext *c,GLVertex *p0,GLVertex *p1,GLVertex *p2);
  293. void gl_draw_line(GLContext *c,GLVertex *p0,GLVertex *p1);
  294. void gl_draw_point(GLContext *c,GLVertex *p0);
  295.  
  296. void gl_draw_triangle_point(GLContext *c, GLVertex *p0,GLVertex *p1,GLVertex *p2);
  297. void gl_draw_triangle_line(GLContext *c, GLVertex *p0,GLVertex *p1,GLVertex *p2);
  298. void gl_draw_triangle_fill(GLContext *c, GLVertex *p0,GLVertex *p1,GLVertex *p2);
  299. void gl_draw_triangle_select(GLContext *c, GLVertex *p0,GLVertex *p1,GLVertex *p2);
  300.  
  301. /* matrix.c */
  302. void gl_print_matrix(const float *m);
  303. /*
  304. void glopLoadIdentity(GLContext *c,GLParam *p);
  305. void glopTranslate(GLContext *c,GLParam *p);*/
  306.  
  307. /* light.c */
  308. void gl_add_select(GLContext *c,unsigned int zmin,unsigned int zmax);
  309. void gl_enable_disable_light(GLContext *c,int light,int v);
  310. void gl_shade_vertex(GLContext *c,GLVertex *v);
  311.  
  312. void glInitTextures(GLContext *c);
  313. void glEndTextures(GLContext *c);
  314. GLTexture *alloc_texture(GLContext *c,int h);
  315.  
  316. /* image_util.c */
  317. void gl_convertRGB_to_5R6G5B(unsigned short *pixmap,unsigned char *rgb,
  318.                              int xsize,int ysize);
  319. void gl_convertRGB_to_8A8R8G8B(unsigned int *pixmap, unsigned char *rgb,
  320.                                int xsize, int ysize);
  321. void gl_resizeImage(unsigned char *dest,int xsize_dest,int ysize_dest,
  322.                     unsigned char *src,int xsize_src,int ysize_src);
  323. void gl_resizeImageNoInterpolate(unsigned char *dest,int xsize_dest,int ysize_dest,
  324.                                  unsigned char *src,int xsize_src,int ysize_src);
  325.  
  326. GLContext *gl_get_context(void);
  327.  
  328. void gl_fatal_error(char *format, ...);
  329.  
  330.  
  331. /* specular buffer "api" */
  332. GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i, 
  333.                               const float shininess);
  334.  
  335. #ifdef DEBUG
  336.     #define dprintf(format, args...)  fprintf(stderr,"In '%s': " format "\n",__FUNCTION__, ##args);
  337. #else
  338.     #define dprintf(format, args...)
  339. #endif
  340.  
  341. /* glopXXX functions */
  342. #define ADD_OP(a,b,c) void glop ## a (GLContext *,GLParam *);
  343. #include "opinfo.h"
  344.  
  345. /* this clip epsilon is needed to avoid some rounding errors after
  346. several clipping stages */
  347.  
  348. #define CLIP_EPSILON (1E-5)
  349.  
  350. static inline int gl_clipcode(float x,float y,float z,float w1)    {
  351.     float w;
  352.     
  353.     w=w1 * (1.0 + CLIP_EPSILON);
  354.     return (x<-w) | ((x>w)<<1) | ((y<-w)<<2) | ((y>w)<<3) | ((z<-w)<<4) | ((z>w)<<5) ;
  355. }
  356.  
  357. #endif /* _tgl_zgl_h_ */
  358.